package com.ddwl.order.controller;


import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Submit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.feign.api.ShopApi;
import com.ddwl.order.dao.UpdateAfterSaleOrderBo;
import com.ddwl.order.service.ShopAfterSaleOrderServer;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.order.ConfirmAfterSaleBo;
import com.ddwl.schema.bo.order.CreateAfterSaleOrderBo;
import com.ddwl.schema.bo.order.QueryAfterSaleOrderListBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.order.AfterSaleOrderCheckVo;
import com.ddwl.schema.vo.order.AfterSaleOrderDetailVo;
import com.ddwl.schema.vo.order.AfterSaleOrderVo;
import com.ddwl.schema.vo.shop.ShopStaffVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 店铺售后相关接口
 *
 * @author wenbin
 */
@Api(tags = "店铺售后")
@RestController
@RequestMapping("/shopAfterSale")
public class ShopAfterSaleController extends BaseController {

    @Resource
    private ShopAfterSaleOrderServer shopAfterSaleOrderServer;
    @Resource
    private ShopApi shopApi;

    @Submit
    @PostMapping("/createAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("创建售后申请")
    public RestVo<String> createAfterSaleOrder(@Valid @RequestBody CreateAfterSaleOrderBo bo) {
        return RestVo.ok(shopAfterSaleOrderServer.create(bo, getUserId(), getUserNameNew()));
    }

    @Submit
    @PostMapping("/updateAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("用户修改售后申请")
    public RestVo updateAfterSaleOrder(@Valid @RequestBody UpdateAfterSaleOrderBo bo) {
        shopAfterSaleOrderServer.updateAfterSale(bo, getUserId());
        return RestVo.ok();
    }

    @Submit
    @PostMapping("/reLogistics")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("后台填写寄回给客户物流信息")
    public RestVo reLogistics(@RequestBody ConfirmAfterSaleBo bo) {
        shopAfterSaleOrderServer.reLogistics(bo);
        return RestVo.ok();
    }

    @PostMapping("/queryUserAfterSaleList")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("查询我的售后订单列表,app小程序")
    public RestVo<PageVo<AfterSaleOrderVo>> queryUserAfterSaleOrder(@Valid @RequestBody PageBo<QueryAfterSaleOrderListBo> pageBo) {
        pageBo.getParam().setUserId(getUserId());
        //pageBo.getParam().setPlatformTag(1);
        return RestVo.ok(shopAfterSaleOrderServer.getPage(pageBo));
    }

    @PostMapping("/queryConsoleAfterSaleList")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("后台查询售后订单列表,后台")
    public RestVo<PageVo<AfterSaleOrderVo>> queryConsoleAfterSaleOrder(@Valid @RequestBody PageBo<QueryAfterSaleOrderListBo> pageBo) {
        List<ShopStaffVo> list = shopApi.shopListForApi(getUserId()).getResult();
        if (!CollectionUtils.isEmpty(list)) {
            pageBo.getParam().setShopIds(list.stream().map(ShopStaffVo::getShopId).collect(Collectors.toList()));
        } else {
            return RestVo.ok();
        }
        pageBo.getParam().setPlatformTag(1);
        return RestVo.ok(shopAfterSaleOrderServer.getPage(pageBo));
    }

    @GetMapping("/queryConsoleAfterSaleDetail/{afterId}")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("查询售后订单明细")
    public RestVo<AfterSaleOrderDetailVo> queryConsoleAfterSaleDetail(@PathVariable String afterId) {
        return RestVo.ok(shopAfterSaleOrderServer.getAfterSaleOrderDetail(afterId));
    }

    @PostMapping("/confirmAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("审批售后订单")
    public RestVo<AfterSaleOrderVo> confirmAfterSaleOrder(@Valid @RequestBody ConfirmAfterSaleBo bo) {
        shopAfterSaleOrderServer.confirmAfterSale(bo);
        return RestVo.ok();
    }

    @PostMapping("/rejectAfterSale")
    @ApiOperation("拒绝售后订单")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<AfterSaleOrderVo> rejectAfterSaleOrder(@Valid @RequestBody ConfirmAfterSaleBo bo) {
        shopAfterSaleOrderServer.rejectAfterSaleOrder(bo);
        return RestVo.ok();
    }

    @PostMapping("/cancelAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("用户取消售后订单")
    public RestVo cancelAfterSaleOrder(@RequestBody ConfirmAfterSaleBo bo) {
        shopAfterSaleOrderServer.cancelAfterSaleOrder(bo, getUserId());
        return RestVo.ok();
    }

    @PostMapping("/updateRemark")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("更新售后订单备注")
    public RestVo updateRemark(@RequestParam ConfirmAfterSaleBo bo) {
        shopAfterSaleOrderServer.updateRemark(bo);
        return RestVo.ok();
    }

    @GetMapping("/getAfterReason")
    @Auth(required = false)
    @ApiOperation("获取售后原因")
    public RestVo<Map<String, String>> getAfterReason() {
        Map<String, String> afterReason = shopAfterSaleOrderServer.getAfterReason();
        return RestVo.ok(afterReason);
    }

    @GetMapping("/afterSaleRefund")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("退款")
    public RestVo afterSaleRefund(@RequestParam("afterId") String afterId, @RequestParam("afterSaleAmount") BigDecimal afterSaleAmount) {
        shopAfterSaleOrderServer.refund(afterId, getUserId(), getUserNameNew(), afterSaleAmount);
        return RestVo.ok();
    }

    @GetMapping("/checkAfterSale/{spuOrderId}")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("检测售后")
    public RestVo<AfterSaleOrderCheckVo> checkAfterSale(@PathVariable String spuOrderId) {
        AfterSaleOrderCheckVo afterSaleOrderCheckVo = shopAfterSaleOrderServer.checkAfterSale(spuOrderId);
        return RestVo.ok(afterSaleOrderCheckVo);
    }

}
